package com.ruoyi.information.service.back.impl;

import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.information.domain.back.ServiceCenterBack;
import com.ruoyi.information.domain.back.SysDict;
import com.ruoyi.information.mapper.back.*;
import com.ruoyi.information.service.back.IStatisticsBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StatisticsBackServiceImpl implements IStatisticsBackService {

    /**
     * 云课堂
     */
    @Autowired
    private CloudClassroomBackMapper classroomMapper;

    /**
     * 服务案例
     */
    @Autowired
    private WenzhangBackMapper wenzhangMapper;

    /**
     * 资讯
     */
    @Autowired
    private ArticleBackMapper articleMapper;

    /**
     * 科技产业服务港下服务资讯
     */
    @Autowired
    private ServiceArticleBackMapper serviceArticleMapper;

    @Autowired
    private SysUserBackMapper userMapper;

    @Autowired
    private SysUserNeedBackMapper userNeedMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private ServiceCenterBackMapper serviceCenterMapper;

    @Autowired
    private SysDictMapper sysDictMapper;

    @Autowired
    private CompanyNeedBackMapper companyNeedMapper;


    /**
     * 云课堂专辑
     */
    @Autowired
    private CloudClassroomProjectMainBackMapper projectMainBackMapper;

    @Override
    public Map statistics(String typeName) {

        String time = null;

        switch (typeName) {
            case "月":
                time = DateUtils.getMonthStart();
                break;
            case "周":
                time = DateUtils.getWeekStart();
                break;
            case "年":
                time = DateUtils.getYearStart();
                break;
        }
        Map<String, Integer> map = new HashMap<>();

//        if (SecurityUtils.getLoginUser().getRoles().contains("admin")) {
        /*云课堂数量*/
        map.put("classroom", classroomMapper.selectCloudClassroomCount(time));
        /*服务案例数量*/
        map.put("wenzhang", wenzhangMapper.selectWenzhangCount(time));
        /*资讯数量*/
        map.put("article", articleMapper.selectArticleCount(time));
        /*服务资讯*/
        map.put("serviceArticle", serviceArticleMapper.selectServiceArticleCount(time));
//        } else {
//            /*云课堂数量*/
//            map.put("classroom", 0);
//            /*服务案例数量*/
//            map.put("wenzhang", 0);
//            /*资讯数量*/
//            map.put("article", 0);
//            /*服务资讯*/
//            map.put("serviceArticle", 0);
//        }
        return map;
    }

    @Override
    public Map userStatistics(String typeName) {
        String time = null;

        switch (typeName) {
            case "月":
                time = DateUtils.getMonthStart();
                break;
            case "周":
                time = DateUtils.getWeekStart();
                break;
            case "年":
                time = DateUtils.getYearStart();
                break;
        }

        Map<String, Integer> map = new HashMap<>();
        /*总数量*/
        map.put("total", userMapper.selectSysUserByAdd(null));
        /*待审核数量*/
        map.put("reviewCount", (int) userNeedMapper.selectSysUserNeedAuditList());
        /*供给方政府数量*/
        map.put("governmentCount", userMapper.selectSysUserBySupply("01"));
        /*供给方产权单位数量*/
        map.put("propertyCount", userMapper.selectSysUserBySupply("02"));
        /*供给方运营单位数量*/
        map.put("operateCount", userMapper.selectSysUserBySupply("03"));
        /*需求方数量*/
        map.put("needCount", userMapper.selectSysUserByNeed());
        /*已审核用户*/
        map.put("examinedCount", userMapper.selectSysUserByExamined());
        /*在线用户数*/
        Collection<String> keys = redisService.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
        map.put("onlineCount", keys.size());
        /*新增用户数*/
        map.put("addCount", userMapper.selectSysUserByAdd(time));
        /*游客数*/
        Collection<String> youke = redisService.keys("XLook:" + "*");
        map.put("lookCount", youke.size());
        return map;
    }

    /**
     * 企业
     *
     * @return {@link Map}
     */
    @Override
    public ArrayList<Object> enterprise(String typeName) {

        String time = null;

        switch (typeName) {
            case "月":
                time = DateUtils.getMonthStart();
                break;
            case "周":
                time = DateUtils.getWeekStart();
                break;
            case "年":
                time = DateUtils.getYearStart();
                break;
        }

        ServiceCenterBack serviceCenter = new ServiceCenterBack();
        serviceCenter.setParentId(0L);
        /*根据产品体系查对应用户*/
        ArrayList<Object> list = new ArrayList<>();
        for (ServiceCenterBack serviceCenterBack : serviceCenterMapper.selectServiceCenterList(serviceCenter)) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", serviceCenterBack.getServiceName());
            map.put("count", userNeedMapper.selectSysUserByServiceCenter(String.valueOf(serviceCenterBack.getId()),time));
            list.add(map);
        }
        return list;
    }

    /**
     * 政府
     *
     * @return {@link ArrayList}<{@link Object}>
     */
    @Override
    public ArrayList<Object> government(String type) {
        String time = null;

        switch (type) {
            case "月":
                time = DateUtils.getMonthStart();
                break;
            case "周":
                time = DateUtils.getWeekStart();
                break;
            case "年":
                time = DateUtils.getYearStart();
                break;
        }

        /*根据经济带查对应用户*/
        ArrayList<Object> list = new ArrayList<>();
        for (SysDict sysDict : sysDictMapper.selectSysDictList("economic_belt")) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", sysDict.getLabel());
            map.put("count", userMapper.selectSysUserByEconomicBelt(sysDict.getValue(),time));
            list.add(map);
        }
        return list;
    }

    /**
     * 专辑
     *
     * @return {@link ArrayList}<{@link Object}>
     */
    @Override
    public ArrayList<Object> album(String type) {
        String time = null;

        switch (type) {
            case "月":
                time = DateUtils.getMonthStart();
                break;
            case "周":
                time = DateUtils.getWeekStart();
                break;
            case "年":
                time = DateUtils.getYearStart();
                break;
        }

        /*根据专辑查对应专辑*/
        ArrayList<Object> list = new ArrayList<>();
        for (SysDict sysDict : sysDictMapper.selectSysDictList("cloud_classroom_type")) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", sysDict.getLabel());
            map.put("count", projectMainBackMapper.selectCloudClassroomProjectMainByRoomType(sysDict.getValue(),time));
            list.add(map);
        }
        return list;
    }

    /**
     * 服务案例
     *
     * @return {@link ArrayList}<{@link Object}>
     */
    @Override
    public ArrayList<Object> serviceCases(String type) {
        String time = null;

        switch (type) {
            case "月":
                time = DateUtils.getMonthStart();
                break;
            case "周":
                time = DateUtils.getWeekStart();
                break;
            case "年":
                time = DateUtils.getYearStart();
                break;
        }

        /*根据类型查对应服务案例*/
        ArrayList<Object> list = new ArrayList<>();
        for (SysDict sysDict : sysDictMapper.selectSysDictList("case_type")) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", sysDict.getLabel());
            map.put("count", wenzhangMapper.selectWenzhangByType(sysDict.getValue(),time));
            list.add(map);
        }
//        list = (ArrayList<Object>) list.stream().sorted((o1,o2)->{
//            Integer v1 =(Integer) ((Map<String, Object>) o1).get("count");
//            Integer v2 =(Integer) ((Map<String, Object>) o2).get("count");
//            return v2.compareTo(v1);
//        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 资讯
     *
     * @return {@link ArrayList}<{@link Object}>
     */
    @Override
    public ArrayList<Object> information(String type) {
        String time = null;

        switch (type) {
            case "月":
                time = DateUtils.getMonthStart();
                break;
            case "周":
                time = DateUtils.getWeekStart();
                break;
            case "年":
                time = DateUtils.getYearStart();
                break;
        }

        /*根据类型查对应资讯*/
        ArrayList<Object> list = new ArrayList<>();
        for (SysDict sysDict : sysDictMapper.selectSysDictList("information_type")) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", sysDict.getLabel());
            map.put("count", articleMapper.selectArticleByArticleType(sysDict.getValue(),time));
            list.add(map);
        }
        return list;
    }

    /**
     * 需求
     *
     * @return {@link ArrayList}<{@link Object}>
     */
    @Override
    public ArrayList<Object> need(String type) {
        String time = null;

        switch (type) {
            case "月":
                time = DateUtils.getMonthStart();
                break;
            case "周":
                time = DateUtils.getWeekStart();
                break;
            case "年":
                time = DateUtils.getYearStart();
                break;
        }

        /*根据类型查对应资讯*/
        ArrayList<Object> list = new ArrayList<>();
        for (SysDict sysDict : sysDictMapper.selectSysDictList("need_type")) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", sysDict.getLabel());
            map.put("count", companyNeedMapper.selectCompanyNeedByNeedCategory(sysDict.getValue(),time));
            list.add(map);
        }
        return list;
    }
}
